home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / 3dlib.zip / 3DLIB.DOC < prev    next >
Text File  |  1988-02-28  |  20KB  |  790 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.                                 3D TRANSFORMS
  26.  
  27.                           A Turbo C Function Library
  28.  
  29.                                  Version 1.00
  30.  
  31.  
  32.  
  33.  
  34.                               Copyright (c) 1988
  35.  
  36.                                  Gus O'Donnell
  37.                                 8301 Mondon Way
  38.                              Orangevale, CA  95662
  39.  
  40.                               All Rights Reserved
  41.  
  42.                                February 29, 1988
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.         Introduction.
  73.  
  74.         3D TRANSFORMS is a library of functions used to create, manipulate
  75.         and display objects in three dimensions.  The functions allow the
  76.         programmer to create representations of solid objects bounded by
  77.         polygons, rotate, translate, and scale the objects in three dimen-
  78.         sions, and (with Turbo C version 1.5) display the objects in color
  79.         with a given light source.
  80.  
  81.         The functions may be classed into the following categories:
  82.  
  83.  
  84.         Initialization functions:
  85.  
  86.             identity    new_face    new_obj
  87.  
  88.         Vector and matrix math functions:
  89.  
  90.             dot_prod    mat_mul     normal
  91.  
  92.         Transformation functions:
  93.  
  94.             scale       trans       xrot        yrot
  95.             zrot
  96.  
  97.         Data structure manipulation functions:
  98.  
  99.             add_corner  add_face    del_face    xform
  100.             max_z       min_z
  101.  
  102.         Display functions:
  103.  
  104.             disp_face   disp_object
  105.  
  106.         Debug functions:
  107.  
  108.             dump_mat    dump_face   dump_obj
  109.  
  110.  
  111.         This library may be used with Turbo C version 1.5, or, with the
  112.         exception of disp_face and disp_object, with version 1.0.
  113.  
  114.         The routines in this library are based on techniques described in
  115.         the book "Principles of Interactive Computer Graphics", by Newman
  116.         and Sproull, McGraw-Hill, publishers.
  117.  
  118.         Turbo C is a registered trademark of Borland International.
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                                      1
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.         Using 3D TRANSFORMS.
  139.  
  140.         Included in 3D TRANSFORMS is a header file, 3D.H, containing function
  141.         prototypes and data structure definitions.  3D TRANSFORMS supports
  142.         the following data structures:
  143.  
  144.  
  145.             MATRIX:  A 4 X 4 matrix is used to describe transformations
  146.                      in three dimensions, such as rotation, scaling, and
  147.                      translation.  Multiple transformations may be con-
  148.                      catenated.  A point in three dimensions is transformed
  149.                      by multiplying it by the transformation matrix.
  150.  
  151.             VECTOR:  An array of three coordinates representing a direction.
  152.                      This is typically used to define the direction of a
  153.                      light source, or the normal to a face.
  154.  
  155.             FACE:    A face describes a flat surface of an object.  It is
  156.                      defined by a list of corners.
  157.  
  158.             OBJECT:  A solid object is defined by a list of faces. Objects
  159.                      are created by defining faces individually, then adding
  160.                      them to the object.
  161.  
  162.  
  163.         To create an object in a program:
  164.  
  165.         1.  Include 3D.H in your program.
  166.  
  167.         2.  Allocate memory for the object and its faces as follows:
  168.  
  169.                     FACE *a;
  170.                     OBJECT *o;
  171.  
  172.                     a = (FACE *)malloc(sizeof(FACE));
  173.                     o = (OBJECT *)malloc(sizeof(OBJECT));
  174.  
  175.         3.  Initialize the data structures as follows:
  176.  
  177.                     new_face (a);
  178.                     new_obj (o);
  179.  
  180.         4.  Add corners to the face.  Corners are added in standard order,
  181.             i.e., in clockwise fashion as viewed from the outside of the
  182.             object:
  183.  
  184.                     add_corner (x1,y1,z1,a);
  185.                     add_corner (x2,y2,z2,a);
  186.  
  187.         5.  Add faces to the object:
  188.  
  189.                     add_face (o,a);
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                      2
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.         6.  Create the transformation matrix.  In this example, the matrix
  205.             includes rotation followed by translation:
  206.  
  207.                      MATRIX m;
  208.  
  209.                      identity (m);
  210.                      xrot(3.14159/4,m);
  211.                      trans(10.0,20.0,30.0,m);
  212.  
  213.         7.  Transform the object:
  214.  
  215.                      xform (*o,m);
  216.  
  217.  
  218.         To display the object, the graphics system must be initialized, and
  219.         the graphics driver resident in the local directory, or in the path
  220.         specified by the initgraph function.  A light source is specified,
  221.         and the object may be transformed with a second matrix:
  222.  
  223.  
  224.                       VECTOR s;
  225.                       MATRIX id;
  226.  
  227.                       identity (id);
  228.                       s[0] = 0.0;
  229.                       s[1] = 0.0;
  230.                       s[2] = 1.0;
  231.                       detectgraph (&g_driver,&g_mode);
  232.                       initgraph (&g_driver,&g_mode,"");
  233.                       disp_object (s,1,o,id);
  234.  
  235.  
  236.         For a complete example, see the demonstration program DEMO3D.C.
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                       3
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.         3D TRANSFORMS Functions.
  271.  
  272.         All function prototypes are in 3D.H.
  273.  
  274.         Name            add_corner - adds a corner to a face.
  275.  
  276.         Usage           int add_corner(double x, double y, double z,
  277.                                        FACE *this_face);
  278.  
  279.         Related
  280.         functions usage int add_face(OBJECT *this_obj, FACE *this_face);
  281.                         int del_face(OBJECT *this_obj, FACE *this_face);
  282.  
  283.         Description     add_corner adds a point defined by [x,y,z] to
  284.                         this_face.  Corners are added in standard order,
  285.                         i.e., clockwise as viewed from the outside of the
  286.                         object.
  287.  
  288.                         add_face adds a face described by this_face to
  289.                         the object this_obj.
  290.  
  291.                         del_face deletes the face pointed to by this_face
  292.                         from this_obj.
  293.  
  294.         Return value    add_corner returns 0 if successful.  1 is returned
  295.                         if memory cannot be allocated for the new corner.
  296.                         2 is returned if the corner is colinear with the
  297.                         last two corners added.  In this case, the previous-
  298.                         ly added corner is replaced with the new corner.
  299.  
  300.                         add_face returns 0 if successful.  1 is returned
  301.                         if the face has less than three corners.  In this
  302.                         case the face is not added to the object.
  303.  
  304.                         del_face returns 0 if successful.  1 is returned
  305.                         if the face is not part of the object.
  306.  
  307.         _____________________________________________________________________
  308.  
  309.  
  310.         Name            add_face - add a face to an object.
  311.  
  312.         Usage           int add_face(OBJECT *this_obj, FACE *this_face);
  313.  
  314.         Description     See add_corner
  315.  
  316.         _____________________________________________________________________
  317.  
  318.  
  319.         Name            del_face - delete a face from an object.
  320.  
  321.         Usage           int del_face(OBJECT *this_obj, FACE *this_face);
  322.  
  323.         Description     See add_corner
  324.  
  325.  
  326.  
  327.                                      4
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.         Name            disp_face - display a face on the screen.
  337.  
  338.         Usage           void disp_face(VECTOR lsource, int color,
  339.                                        FACE *this_face, MATRIX xfrm_mat);
  340.  
  341.         Related
  342.         functions usage void disp_object(VECTOR lsource, int color,
  343.                                       OBJECT *this_obj, MATRIX xfrm_mat);
  344.  
  345.         Description     disp_face displays a face on the screen.  The
  346.                         face is first transformed by xfrm_mat (the structure
  347.                         is unchanged).  The color is defined by color,
  348.                         and the intensity of the shading is proportional
  349.                         to the normalized dot product of the face normal
  350.                         and lsource.  If the normal to the face has a
  351.                         negative z component, it is not displayed.  The
  352.                         face also is not displayed if it has fewer than
  353.                         three corners.  The graphics system must be
  354.                         initialized prior to calling this function.
  355.  
  356.                         disp_obj displays an object on the screen.  The
  357.                         function simply calls disp_face for each face in
  358.                         the object.
  359.  
  360.                         Both of these functions require Turbo C version
  361.                         1.5.
  362.  
  363.         Return value    Neither function returns a value.
  364.  
  365.         _____________________________________________________________________
  366.  
  367.  
  368.         Name            disp_object - display an object on the screen.
  369.  
  370.         Usage           void disp_object(VECTOR lsource, int color,
  371.                                       OBJECT *this_obj, MATRIX xfrm_mat);
  372.  
  373.         Description     See disp_face
  374.  
  375.         _____________________________________________________________________
  376.  
  377.  
  378.         Name            dot_prod - calculate the dot product of two
  379.                                    vectors.
  380.  
  381.         Usage           double dot_prod(VECTOR vec1, VECTOR vec2);
  382.  
  383.         Description     dot_prod returns the normalized inner product
  384.                         (dot product or scalar product) of two vectors.
  385.                         Prior to the calculation, both vectors are norm-
  386.                         alized, i.e., adjusted to unit length.  Thus,
  387.                         the value returned is equal to the cosine of the
  388.                         angle between the two vectors.
  389.  
  390.  
  391.  
  392.  
  393.                                       5
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.         Return value    The function returns a double precision value
  403.                         equal to the cosine of the angle between the two
  404.                         vectors.
  405.  
  406.         _______________________________________________________________________
  407.  
  408.  
  409.         Name            dump_face - dump the contents of the face data
  410.                                     structure to the screen.
  411.  
  412.         Usage           void dump_face(FACE this_face);
  413.  
  414.         Related
  415.         functions usage void dump_mat(MATRIX this_mat);
  416.                         void dump_obj(OBJECT this_obj);
  417.  
  418.         Description     dump_face displays the contents of the data
  419.                         structure this_face to the screen.  This
  420.                         function is useful for debugging.
  421.  
  422.                         dump_mat displays the elements of a matrix.
  423.  
  424.                         dump_obj displays the contents of the data
  425.                         structure this_obj by calling dump_face for
  426.                         each face in the object.
  427.  
  428.         Return value    None of these functions returns a value.
  429.  
  430.         _____________________________________________________________________
  431.  
  432.  
  433.         Name            dump_mat - dump the contents of a matrix to
  434.                                    the screen.
  435.  
  436.         Usage           void dump_mat(MATRIX this_mat);
  437.  
  438.         Description     See dump_face
  439.  
  440.         _____________________________________________________________________
  441.  
  442.  
  443.         Name            dump_obj - dump the contents of the object data
  444.                                    structure to the screen.
  445.  
  446.         Usage           void dump_obj(OBJECT this_obj);
  447.  
  448.         Description     See dump_face
  449.  
  450.         _______________________________________________________________________
  451.  
  452.  
  453.         Name            identity - initialize a matrix.
  454.  
  455.         Usage           void identity(MATRIX this_mat);
  456.  
  457.  
  458.  
  459.                                       6
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.         Description     identity assigns a value to each element of
  469.                         this_mat.  The diagonal elements are assigned
  470.                         a value of 1.0.  Off diagonal elements are
  471.                         assigned a value of 0.
  472.  
  473.         Return value    This function does not return a value.
  474.  
  475.         _______________________________________________________________________
  476.  
  477.  
  478.         Name            mat_mul - multiply two matrices.
  479.  
  480.         Usage           void mat_mul(MATRIX mat1, MATRIX mat2,
  481.                                      MATRIX mat3);
  482.  
  483.         Description     mat_mul multiplies two 4 X 4 matrices mat1 and
  484.                         mat2 and assigns the result to mat3.
  485.  
  486.         Return value    This function does not return a value.
  487.  
  488.         _______________________________________________________________________
  489.  
  490.  
  491.         Name            max_z - return the maximum z coordinate in the
  492.                                 face.
  493.  
  494.         Usage           double max_z(FACE *this_face);
  495.  
  496.         Related
  497.         functions usage double min_z(FACE *this_face);
  498.  
  499.         Description     max_z returns the value of the largest z
  500.                         coordinate of any corner in the face.
  501.  
  502.                         min_z returns the value of the smallest z
  503.                         coordinate of any corner in the face.
  504.  
  505.         Return value    max_z returns a double precision value equal
  506.                         to the maximum z coordinate of the face.
  507.  
  508.                         min_z returns a double precision value equal
  509.                         to the minimum z coordinate of the face
  510.  
  511.         _____________________________________________________________________
  512.  
  513.  
  514.         Name            min_z - return the minimum z coordinate in the
  515.                                 face.
  516.  
  517.         Usage           double min_z(FACE *this_face);
  518.  
  519.         Description     See max_z
  520.  
  521.  
  522.  
  523.  
  524.  
  525.                                       7
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.         Name            new_face - initialize a face.
  535.  
  536.         Usage           int new_face(FACE *this_face);
  537.  
  538.         Related
  539.         functions usage int new_obj(OBJECT *this_obj);
  540.  
  541.         Description     new_face initializes the data structure for a face.
  542.                         The data structure must be initialized before adding
  543.                         corners.
  544.  
  545.                         new_obj initializes the data structure for an object.
  546.                         The data structure must be initialized before adding
  547.                         faces.
  548.  
  549.         Return value    Either function returns a 0 if successful.  Either
  550.                         returns 1 if memory cannot be allocated.
  551.  
  552.         _______________________________________________________________________
  553.  
  554.  
  555.         Name            new_obj - initialize an object.
  556.  
  557.         Usage           int new_obj(OBJECT *this_obj);
  558.  
  559.         Description     See new_face
  560.  
  561.         _______________________________________________________________________
  562.  
  563.  
  564.         Name            normal - calculate the normal to a face.
  565.  
  566.         Usage           int normal(FACE *this_face, VECTOR norm);
  567.  
  568.         Description     normal calculates the normal vector of this_face
  569.                         and assigns it to norm.  The function assumes that
  570.                         the corners have been added in standard order, and
  571.                         that the polygon is convex.
  572.  
  573.         Return value    The function returns 0 if the operation is successful.
  574.                         The function returns 1 if the face has fewer than
  575.                         three corners.
  576.  
  577.         _______________________________________________________________________
  578.  
  579.  
  580.         Name            scale - add scaling to a transformation matrix.
  581.  
  582.         Usage           void scale(double sx, double sy, double sz,
  583.                                    MATRIX this_mat);
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.                                       8
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.         Related
  601.         functions usage void trans(double tx, double ty, double tz,
  602.                                    MATRIX this_mat);
  603.                         void xrot(double theta, MATRIX this_mat);
  604.                         void yrot(double theta, MATRIX this_mat);
  605.                         void zrot(double theta, MATRIX this_mat);
  606.  
  607.         Description     scale adds scaling to the transformation matrix.
  608.                         Each axis may be scaled differently.  When a point
  609.                         is scaled, the x coordinate is multiplied by sx, and
  610.                         so on.
  611.  
  612.                         trans adds translation to the transformation matrix.
  613.                         When a point is translated, tx is added to the x
  614.                         coordinate, and so on.
  615.  
  616.                         xrot adds rotation about the x axis to the trans-
  617.                         formation matrix.  Similarly, yrot and zrot add
  618.                         rotation about the y axis and z axis respectively.
  619.  
  620.         Return value    None of these functions returns a value.
  621.  
  622.         _______________________________________________________________________
  623.  
  624.  
  625.         Name            trans - add translation to the transformation
  626.                                 matrix.
  627.  
  628.         Usage           void trans(double tx, double ty, double tz,
  629.                                    MATRIX this_mat);
  630.  
  631.         Description     See scale
  632.  
  633.         _______________________________________________________________________
  634.  
  635.  
  636.         Name            xform - transform an object.
  637.  
  638.         Usage           int xform(OBJECT this_obj, MATRIX transform);
  639.  
  640.         Description     xform transforms an object by multiplying every
  641.                         vertex in the object by the transformation matrix.
  642.  
  643.         Return value    The function always returns a 0.
  644.  
  645.         _______________________________________________________________________
  646.  
  647.  
  648.         Name            xrot - add rotation about the x axis to the trans-
  649.                                formation matrix.
  650.  
  651.         Usage           void xrot(double theta, MATRIX this_mat);
  652.  
  653.         Description     See scale
  654.  
  655.  
  656.  
  657.                                       9
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.  
  665.  
  666.         Name            yrot - add rotation about the y axis to the trans-
  667.                                formation matrix.
  668.  
  669.         Usage           void yrot(double theta, MATRIX this_mat);
  670.  
  671.         Description     See scale
  672.  
  673.         _______________________________________________________________________
  674.  
  675.  
  676.         Name            zrot - add rotation about the z axis to the trans-
  677.                                formation matrix.
  678.  
  679.         Usage           void zrot(double theta, MATRIX this_mat);
  680.  
  681.         Description     See scale
  682.  
  683.  
  684.  
  685.  
  686.  
  687.  
  688.  
  689.  
  690.  
  691.  
  692.  
  693.  
  694.  
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.                                       10
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.                                    NOTICE
  733.  
  734.         3D TRANSFORMS is protected by copyright.  The functions in this
  735.         library may be used for your own personal use, but may NOT be
  736.         resold or used for any other commercial purpose, or included in
  737.         any commercial product without written permission from the author.
  738.         You may copy and distribute this product freely, provided 1) it
  739.         is reproduced in its entirety, including documentation and exam-
  740.         ples, and 2) you do not charge for copies (other than a nominal
  741.         copying fee to cover materials).
  742.  
  743.         You may obtain permission to use 3D TRANSFORMS commercially,
  744.         along with complete source code, by sending $25.00 U.S. to:
  745.  
  746.  
  747.                             Gus O'Donnell
  748.                             8301 Mondon Way
  749.                             Orangevale, CA  95662
  750.  
  751.  
  752.         Please send comments and suggestions to this same address, or
  753.         leave a message on Compuserve, user ID 76214,1554.
  754.  
  755.  
  756.  
  757.  
  758.  
  759.  
  760.  
  761.  
  762.  
  763.  
  764.  
  765.  
  766.  
  767.  
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.                                       11
  790.